package Z.dailyExercise._0225;

public class _剑指Offer29顺时针打印矩阵 {
    /**
     * 借助题解：设计边界 根据边界打印
     * @param matrix
     * @return
     */
    public int[] spiralOrder(int[][] matrix) {
        if (matrix.length==0){
            return new int[0];
        }
        int m= matrix.length;
        int n = matrix[0].length;
        int[] res = new int[m*n];

        int l=0,r=n-1,t=0,b=m-1,count =0;
        while (true){
            for (int i = l; i <= r; i++) {
                res[count++] = matrix[t][i];
            }
            if (++t > b){
                break;
            }
            for (int i = t; i <= b; i++) {
                res[count++] = matrix[i][r];

            }
            if (--r <l){
                break;
            }
            for (int i = r; i >= l; i--) {
                res[count++] = matrix[b][i];

            }
            if (--b < t){
                break;
            }
            for (int i = b; i >= t ; i--) {
                res[count++] = matrix[i][l];

            }
            if (++l>r){
                break;
            }

        }

        return res;

    }
}
